package com.mall.server.service.pms;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.PmsProductAttributeApi;
import com.mall.dubbo.api.PmsProductAttributeCategoryApi;
import com.mall.model.domain.PmsProductAttribute;
import com.mall.model.domain.PmsProductAttributeCategory;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.ProductAttributeCategoryWithAttrVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PmsProductAttributeCategoryService {

    @DubboReference
    private PmsProductAttributeCategoryApi productCategoryApi;

    @DubboReference
    private PmsProductAttributeApi attributeApi;

    public void addProductAttributeCategory(String name) {
        PmsProductAttributeCategory newProductAc = new PmsProductAttributeCategory();
        PmsProductAttributeCategory productAttributeCategory = productCategoryApi.findProductCategoryByName(name);
        if (productAttributeCategory!=null){
            return;
        }
        newProductAc.setName(name);
        newProductAc.setParamCount(0);
        newProductAc.setAttributeCount(0);
        productCategoryApi.saveProductCategory(newProductAc);
    }

    public void deleteCategory(Long id) {
        PmsProductAttributeCategory pmsProductAttributeCategory=productCategoryApi.findProductCategoryById(id);
        if (pmsProductAttributeCategory==null){
            return;
        }
        productCategoryApi.deleteCategory(id);
    }

    public CommonPage<PmsProductAttributeCategory> selectList(Integer pageNum,Integer pageSize) {
        Page<PmsProductAttributeCategory> list=productCategoryApi.selectByPage(pageNum,pageSize);
        CommonPage<PmsProductAttributeCategory> pageInfo = new CommonPage<>(list);
        return pageInfo;
    }

    public List<ProductAttributeCategoryWithAttrVo> selectProductCategoryWithAttr() {
        List<PmsProductAttributeCategory> list=productCategoryApi.selectAll();
        List<Long> ids = CollUtil.getFieldValues(list, "id", Long.class);
        List<PmsProductAttribute> productAttributeList=new ArrayList<>();
        List<ProductAttributeCategoryWithAttrVo> vos = new ArrayList<>();
        for (Long id : ids) {
            productAttributeList= attributeApi.findByAttributeCategoryId(id);
            PmsProductAttributeCategory category = productCategoryApi.findProductCategoryById(id);
            if (productAttributeList.size()!=0){
                ProductAttributeCategoryWithAttrVo vo = ProductAttributeCategoryWithAttrVo.init(productAttributeList, category);
                vos.add(vo);
            }
        }
        return vos;
    }

    public void updateCategory(Integer id, String name) {
        PmsProductAttributeCategory category = productCategoryApi.findProductCategoryById(Convert.toLong(id));
        if(category!=null){
            category.setName(name);
            productCategoryApi.updateCategory(category);
        }
    }

    public PmsProductAttributeCategory findCategoryById(Long id) {
        PmsProductAttributeCategory category = productCategoryApi.findProductCategoryById(id);

        return category;
    }
}
